using Godot;
using Godot.Collections;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;

public partial class Movement : Node2D,IComponent
{
    private const int SPEED = 3;
	private Vector2I _currentDirection;
    private Characters _character;
    private Area2D _floorCheck;
    private bool isFloor;
    public Array<Node2D> _floor = new Array<Node2D>();
	// Called when the node enters the scene tree for the first time.
	public void Initialize()
    {
        _character = GetOwner<Characters>();
        _floorCheck = _character.GetNode<Area2D>("FloorCheck");
        _floorCheck.BodyExited += OnFloorCheckExited;
        _floorCheck.BodyEntered += OnFloorCheckEntered;
    }

	public void Move(Vector2I direction,double delta)
	{
		_character = GetOwner<Characters>();
        _floorCheck = _character.GetNode<Area2D>("FloorCheck");
        var movedirection = Math.Atan2(-1*direction.X, direction.Y);
        _floorCheck.Rotation = (float)(movedirection);
        
        if(direction != Vector2I.Zero&&_floor.Count != 0)
        {
            _character.CharacterData.speed += _character.CharacterData.ACCELERATION ;
        }
        else{ _character.CharacterData.speed = 0;}
        if( _character.CharacterData.speed >  _character.CharacterData.MAX_SPEED)
        { _character.CharacterData.speed =  _character.CharacterData.MAX_SPEED;}
        _currentDirection = (int)_character.CharacterData.speed * direction;
        //_character.GlobalPosition += _currentDirection;
        _character.Velocity = _currentDirection;
        //GD.Print(_currentDirection);
        _character.MoveAndSlide();
        
	}

    public void Update(double delta)
    {
    }
    public bool IsBlockV()//垂直碰撞检测
    {
        var spaceState = GetWorld2D().DirectSpaceState;
        //var owner = GetOwner<Node2D>();

        var target = _character.Position + new Vector2(0, 20);//检测目标，y数值与对象尺寸有关

        var query = PhysicsRayQueryParameters2D.Create(_character.Position, target);
        var result = spaceState.IntersectRay(query);

        if(result.Count==0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public bool IsBlockH(Vector2I direction)//水平碰撞检测
    {
        var spaceState = GetWorld2D().DirectSpaceState;
        var owner = GetOwner<Node2D>();
        var target = owner.Position + new Vector2(10, 0);
        var query = PhysicsRayQueryParameters2D.Create(owner.Position, target);
        var result = spaceState.IntersectRay(query);

        for(int i = -18;i <= 18;i++)
        {
            target = owner.Position + new Vector2(10*direction.X, i);
            query = PhysicsRayQueryParameters2D.Create(owner.Position, target);
            result = spaceState.IntersectRay(query);
            if(result.Count!=0)
            {
                break;
            }
        }
        result = spaceState.IntersectRay(query);

        if(result.Count==0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public void OnFloorCheckEntered(Node2D body)
    {
        _floor.Add(body);
    }
    public void OnFloorCheckExited(Node2D body)
    {
        _floor.Remove(body);
    }
}